package com.kehutong.house.admin;
import com.kehutong.common.permission.DataPermissionQuery;
import com.kehutong.common.permission.PermissionNameDto;
import com.kehutong.common.util.Token;
import com.kehutong.house.entity.Community;
import com.kehutong.house.entity.CommunityMerchant;
import com.kehutong.house.service.CommunityMerchantService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;

import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

@MvcAction
@ReqMapping("/admin/basic/community/merchant")
public class CommunityMerchantController {
    @Inject
    private JdbcSession sqlSession;
    @Inject
    private CommunityMerchantService communityMerchantService;

    public final static String[] EXCEL_IMPORT_HEADER = {"小区编号","小区名称","商户号ID","商户号编号","商户名称"};
    @ReqMapping("/list")
    public Object list(Token token, JSONObject jsonObject) {
        FindPage<CommunityMerchant> find = sqlSession.findPage(CommunityMerchant.class)
                .eq(jsonObject, "communityNo",  "merchantSettingId", "mchId")
                .like(jsonObject, "communityName", "mchName")
                .eq("deleted", false)
                .order("createTime", "DESC")
                .page(jsonObject);

        PermissionNameDto dto = new PermissionNameDto();
        dto.setManageDepartmentNo("departmentNo");
        DataPermissionQuery.setManageDataSql(find, dto, null);

        return find.exe();
    }

    @ReqMapping("/get")
    public Object getById(CommunityMerchant communityMerchant) {
        if (communityMerchant == null || communityMerchant.isDeleted()) {
            return Result.error(0, "对象不存在");
        }
        return communityMerchant;
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(CommunityMerchant communityMerchant) {

        // 增加校验不允许有相同的小区名称
        CommunityMerchant item = sqlSession.findOne(CommunityMerchant.class)
                .eq("communityName", communityMerchant.getCommunityName())
                .eq("deleted", false)
                .exe();
        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前小区名称，请修改后重试");
        }

        Community com = Pool.get(Community.class, communityMerchant.getCommunityNo());
        communityMerchant.setDepartmentNo(com.getParent_id());
        communityMerchant.setDepartmentNos(com.getParent_ids());

        sqlSession.insert(communityMerchant);
        return Result.success(communityMerchant);
    }

    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(CommunityMerchant communityMerchant) throws Exception {
        // 增加校验不允许有相同的小区名称
        CommunityMerchant item = sqlSession.findOne(CommunityMerchant.class)
                .eq("communityName", communityMerchant.getCommunityName())
                .noEq("id", communityMerchant.getId())
                .eq("deleted", false)
                .exe();

        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前小区名称，请修改后重试");
        }

        Community com = Pool.get(Community.class, communityMerchant.getCommunityNo());
        communityMerchant.setDepartmentNo(com.getParent_id());
        communityMerchant.setDepartmentNos(com.getParent_ids());

        sqlSession.updateById(communityMerchant);
        return Result.success();
    }

    @ReqMapping("/delete")
    public Object deleteById(CommunityMerchant communityMerchant) {
        if (communityMerchant != null) {
            communityMerchant.setDeleted(Boolean.TRUE);
            sqlSession.updateById(communityMerchant);
        }

        return Result.success();
    }
}
